#include <MoveUp.h>
#include "Branch.h"
#include "Config.h"
#include "HelloWorldScene.h"

USING_NS_CC;

/****************************************************
* Name			: Constructor
* Param			:
* Mission		: Constructor for MoveUp class
* Modify date	: 
* Author		: Phuoctd
****************************************************/
MoveUp::MoveUp()
{
	setName("MoveUp");
	mSizeScreen = CCDirector::sharedDirector()->getWinSize();
}

/****************************************************
* Name			: Update
* Param			: float delta 
* Mission		: Alway update Branch
* Modify date	: 15/5/2014
* Author		: Phuoctd
****************************************************/
void MoveUp::update(float delta)
{
	mParent = (Branch * ) getOwner();
	if(mParent== NULL )
	{
		return;
	}

	switch(mParent->mStatus)
	{
	case isIdle:
		fMoveToTop();
		break;
	case isRunningToTop:
		break;
	case isMax:
		fIsMax();
		break;
	case isCollision:
		break;
	default:
		break;
	}

}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void MoveUp::fMoveToTop()
{
	
	mParent->mStatus = isRunningToTop;
	if(mParent->getPositionY() >= mSizeScreen.height+20)
	{
		fIsMax();
	}
	else
	{		
		CCMoveTo * move = CCMoveTo::create(fDurationPerLevel(mParent->mLevel) , ccp(mParent->getPositionX(), mParent->getPositionY() + 30 ) );
		mSequenceMoveToTop = CCSequence::createWithTwoActions( move, CCCallFuncO::create(move,callfuncO_selector(MoveUp::fIsMoveToTopCallBack),(CCObject*)mParent) );
		mParent->runAction(mSequenceMoveToTop);
	}
	
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void MoveUp::fIsMax()
{
	if(mSequenceMoveToTop!=NULL)
	{
		mParent->stopAction(mSequenceMoveToTop);
	}
	int randomX = rand();
	if(randomX %2 == 0)
	{
		randomX = mSizeScreen.width/2 - kPosition;
	}
	else
	{
		randomX = mSizeScreen.width/2 + kPosition;
	}
	mParent->setPosition(ccp( randomX , -100));
	mParent->mStatus = isIdle;
	mParent->mIsPointed = 0;
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void MoveUp::fIsMoveToTopCallBack(CCObject  *sender)
{
	(( Branch *)sender)->mStatus = isIdle;
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void MoveUp::fIsMaxCallBack(CCObject  *sender)
{
	
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
float MoveUp::fDurationPerLevel(int level)
{
	switch(level)
	{
	case 1:
		return 0.35f;
	case 2:
		return 0.3f;
	case 3:
		return 0.25f;
	case 4:
		return 0.2f;
	case 5:
		return 0.15f;
	default:
		return 0.1f;
	}
}

/****************************************************
* Name			:
* Param			:
* Mission		:
* Modify date	: 
* Author		: Phuoctd
****************************************************/
void MoveUp::fStopAction()
{
	mParent = (Branch * ) getOwner();
	if(mParent== NULL )
	{
		return;
	}

	mParent->stopAction(mSequenceMoveToTop);

}